cssselector: Use a GtkArray to count exact matches
authorTimm Bäder <mail@baedert.org>
Wed, 29 Apr 2020 08:01:25 +0000 (10:01 +0200)
committerTimm Bäder <mail@baedert.org>
Tue, 5 May 2020 06:20:10 +0000 (08:20 +0200)
The list here is at most 3 elements long in my tests.

gtk/gtkarrayimplprivate.h
gtk/gtkcssselector.c

index 0027aa5b3e82071071a2e97deddb30b9d3013961..cfc497e1abdd64acd3135321d95130761b7173d8 100644 (file)
@@ -96,5 +96,14 @@ gtk_array_free (GtkArray       *self,
   g_ptr_array_free (self->ptr_array, TRUE);
 }
 
+static inline void **
+gtk_array_get_data (GtkArray *self)
+{
+  if (G_LIKELY (!self->ptr_array))
+    return self->stack_space;
+
+  return self->ptr_array->pdata;
+}
+
 
 #endif
index 1c1d0c40467ec7876ffa568fdd11fa914b15966c..5c1572e038c0092c7492a4ef93ef63d1f3a8eb7b 100644 (file)
@@ -24,6 +24,7 @@
 
 #include "gtkcssprovider.h"
 #include "gtkstylecontextprivate.h"
+#include "gtkarrayimplprivate.h"
 
 #include <errno.h>
 #if defined(_MSC_VER) && _MSC_VER >= 1500
@@ -2119,7 +2120,8 @@ subdivide_infos (GByteArray                 *array,
   GHashTableIter iter;
   guint max_count;
   gpointer key, value;
-  GPtrArray *exact_matches;
+  void *exact_matches_stack[8];
+  GtkArray exact_matches_array;
   gint32 res;
   guint i;
 
@@ -2161,7 +2163,7 @@ subdivide_infos (GByteArray                 *array,
   matched_infos = g_alloca (sizeof (GtkCssSelectorRuleSetInfo *) * n_infos);
   remaining_infos = g_alloca (sizeof (GtkCssSelectorRuleSetInfo *) * n_infos);
 
-  exact_matches = NULL;
+  gtk_array_init (&exact_matches_array, (void**)exact_matches_stack, 8);
   for (i = 0; i < n_infos; i++)
     {
       GtkCssSelectorRuleSetInfo *info = infos[i];
@@ -2172,9 +2174,7 @@ subdivide_infos (GByteArray                 *array,
          if (info->current_selector == NULL)
            {
              /* Matches current node */
-             if (exact_matches == NULL)
-               exact_matches = g_ptr_array_new ();
-             g_ptr_array_add (exact_matches, info->match);
+              gtk_array_add (&exact_matches_array, info->match);
              if (info->selector_match != NULL)
                *info->selector_match = GUINT_TO_POINTER (tree_offset);
            }
@@ -2191,13 +2191,14 @@ subdivide_infos (GByteArray                 *array,
        }
     }
 
-  if (exact_matches)
+  if (exact_matches_array.len > 0)
     {
-      g_ptr_array_add (exact_matches, NULL); /* Null terminate */
+      gtk_array_add (&exact_matches_array, NULL); /* Null terminate */
       res = array->len;
-      g_byte_array_append (array, (guint8 *)exact_matches->pdata,
-                          exact_matches->len * sizeof (gpointer));
-      g_ptr_array_free (exact_matches, TRUE);
+      g_byte_array_append (array, (guint8 *)gtk_array_get_data (&exact_matches_array),
+                           exact_matches_array.len * sizeof (gpointer));
+
+      gtk_array_free (&exact_matches_array, NULL);
     }
   else
     res = GTK_CSS_SELECTOR_TREE_EMPTY_OFFSET;